package com.project.running.web.Servlet;

import com.project.running.Exception.CustomerException;
import com.project.running.Service.Impl.LoginServiceImpl;
import com.project.running.Service.LoginService;
import com.project.running.bean.Memberinfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/10/5-10-05-20:29
 * @Description：com.project.running.web.Servlet
 */
//@WebServlet("/login")
    @RequestMapping("")
@Slf4j
@Controller
public class login {
    @Autowired
    LoginService service ;

    @PostMapping("/login")
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //req.getParameter("")

        String username = req.getParameter("username");       //用户名
        String password = req.getParameter("password");          //密码
        String verifyCode = (String) req.getSession().getAttribute("verifyCode");     //验证码
        String authCode = req.getParameter("authCode");        //用户输入的验证码
        String autoLogin = req.getParameter("autoLogin");

        if(autoLogin != null){                 //用户勾选了自动登录
            Cookie cookie = new Cookie("username", username);
            Cookie cookie1 = new Cookie("password",password);
            cookie.setMaxAge(60*60*24);
            cookie1.setMaxAge(60*60*24);
            resp.addCookie(cookie);
            resp.addCookie(cookie1);
        }
        log.debug("username = " + username);
        log.debug("password = " + password);
        log.debug("verifyCode = " + verifyCode);
        log.debug("authCode = " + authCode);

        Memberinfo member = service.login(username, password, verifyCode, authCode);
        if(member != null){
            req.getSession().setAttribute("memberInfo",member);
            resp.sendRedirect("member/activity.jsp");
        }
    }

//    @GetMapping("/login")
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        doPost(req, resp);
//    }
}
